package com.kiln.juxingqs.ui.kchart

import android.content.Intent
import android.os.Handler
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.LogUtils
import com.google.android.material.tabs.TabLayout
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.chart.SimpleFragmentPagerAdapter
import com.kiln.juxingqs.databinding.ActivityStockDetaillandBinding
import com.kiln.juxingqs.fragment.market.*
import com.kiln.juxingqs.ui.vm.KLineModel
import com.kiln.juxingqs.utils.DateUtils

/**
 * Description K线图
 * Author puyantao
 * Email 1067899750@qq.com
 * Date 2018-11-30 10:27
 */
class StockDetailLandActivity : BaseMiddleActivity<KLineModel, ActivityStockDetaillandBinding>() {

    open var goodsId: String? = null
    private var zd_float: String? = null
    private var landscape: Boolean = false
    var curPos: Int = 0
    val handler = Handler()

    override fun getLayout(): Int = R.layout.activity_stock_detailland

    override fun initViewModel(): KLineModel = createViewModel()

    override fun initView() {
        super.initView()
        binding.event = this
        binding.vm = viewModel

        goodsId = intent.getStringExtra("goodsId")!!
        zd_float = intent.getStringExtra("zd_float")!!
        landscape = intent.getBooleanExtra("landscape", false)

//        handler.postDelayed(runnable, 0) //每几秒执行一次runnable. -第二步

//        val fragments = arrayOf<Fragment>(
//            ChartOneDayFragment.newInstance(landscape, goodsId!!)!!, ChartKLineMinute1Fragment.newInstance(5, false, goodsId!!),
//            ChartFiveDayFragment.newInstance(false, goodsId!!)!!, ChartKLineFragment.newInstance(1, false, goodsId!!),
//            ChartKLine7Fragment.newInstance(7, false, goodsId!!), ChartKLine30Fragment.newInstance(30, false, goodsId!!),
//        )

        val fragments = arrayOf<Fragment>(
            ChartOneDayFragment.newInstance(true, goodsId!!)!!,
            ChartMinuteMoreKLineFragment.newInstance(5, true, goodsId!!),
//            ChartFiveDayFragment.newInstance(true, goodsId!!)!!,
            ChartMoreKLineFragment.newInstance(1, true, goodsId!!),
            Chart7MoreKLineFragment.newInstance(7, true, goodsId!!),
            Chart30MoreKLineFragment.newInstance(30, true, goodsId!!),

//            ChartKLineMinute5Fragment.newInstance(5, false, goodsId!!),
        )
//        val titles = arrayOf("分时", "1分", "五日", "日K", "周K", "月K") //
        val titles = arrayOf("分时", "1分", "日K", "周K", "月K") //
        binding.viewPager.setOffscreenPageLimit(fragments.size)
        binding.viewPager.setAdapter(
            SimpleFragmentPagerAdapter(
                supportFragmentManager,
                fragments,
                titles
            )
        )
        binding.tab.setupWithViewPager(binding.viewPager)

        binding.tab.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                var pos: Int = tab.getPosition()
                curPos = pos
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {}
            override fun onTabReselected(tab: TabLayout.Tab?) {}
        })
        binding.llClose.setOnClickListener {
            startActivity(Intent(this, StockDetailActivity::class.java).apply {
                putExtra("goodsId", "${goodsId}")
                putExtra("zd_float", "${zd_float}")
            })
            finish()
        }

    }

//    val runnable: Runnable = object : Runnable {
//        override fun run() {
//            //要做的事情
//            if (DateUtils.isBelong(viewModel.begin_time, viewModel.end_time)) {
//                viewModel.holeKLineHead(goodsId)
//                //实时刷新
//                if (curPos == 0) { //分时
//                    viewModel.kType.value = 0
//                    viewModel.holeOneDay(goodsId!!)
//                } else if (curPos == 2) { //五日
//                    viewModel.kType.value = 1
//                    viewModel.hole5Day(goodsId!!)
//                }
//                viewModel.isOpenTime.value = true
//                LogUtils.i("在${viewModel.begin_time} - ${viewModel.end_time}，时间范围内")
//            } else {
////                LogUtils.i("非开盘时间范围内")
//                viewModel.isOpenTime.value = false
//            }
//
//            handler.postDelayed(this, 5000) //每5秒执行一次runnable. -第一步
//
//        }
//    }

    override fun observerUI() {
        super.observerUI()

    }

    override fun onDestroy() {
        super.onDestroy()
//        handler.removeCallbacks(runnable);
    }

}